package com.onpositive.realmview.model.realm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import com.onpositive.semantic.model.realm.HashDelta;
import com.onpositive.semantic.model.realm.IIdentifiableObject;
import com.onpositive.semantic.model.realm.IModifiableRealm;
import com.onpositive.semantic.model.realm.OrderedRealm;

public abstract class IDRealm<T extends IIdentifiableObject> extends
		OrderedRealm<T> {
	ArrayList<T> mn;
	
	
	public int hashCode() {		
		return super.hashCode();
	}
	

	
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IDRealm other = (IDRealm) obj;
		if (collection == null) {
			if (other.collection != null)
				return false;
		} else if (!collection.equals(other.collection))
			return false;
		return true;
	}

	public IDRealm() {
		mn=collection;
	}
	
	public void save(DataOutputStream stream) throws IOException {
		Collection<T> contents = getContents();
		stream.writeInt(contents.size());
		for (IIdentifiableObject c : contents) {
			stream.writeUTF(c.getId());
		}
	}
	
	

	public void load(DataInputStream stream) throws IOException {
		int readInt = stream.readInt();
		ArrayList<T> list = new ArrayList<T>();
		for (int a = 0; a < readInt; a++) {
			String readUTF = stream.readUTF();
			T lookup = lookup(readUTF);
			if (lookup != null) {
				list.add(lookup);
			}
		}
		collection = list;
		mn=list;
	}

	protected abstract T lookup(String id);

	public void set(IModifiableRealm<T> columns) {
		collection=new ArrayList<T>(columns.getContents());
	}
}
